package com.ruoyi.project.poem.poemset.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.poem.poemset.domain.PoemSet;
import com.ruoyi.project.poem.poemset.mapper.PoemSetMapper;
import com.ruoyi.project.poem.poemset.service.IPoemSetService;
import com.ruoyi.project.poem.poemsetdetail.domain.PoemSetDetail;
import com.ruoyi.project.poem.poemsetdetail.mapper.PoemSetDetailMapper;

/**
 * 诗词诗单Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-03-30
 */
@Service
@DataSource(DataSourceType.POEM)
public class PoemSetServiceImpl implements IPoemSetService {

	@Autowired
	private PoemSetMapper poemSetMapper;

	@Autowired
	private PoemSetDetailMapper poemSetDetailMapper;

	/**
	 * 查询诗词诗单
	 * 
	 * @param id 诗词诗单ID
	 * @return 诗词诗单
	 */
	@Override
	public PoemSet selectPoemSetById(Long id) {
		return poemSetMapper.selectPoemSetById(id);
	}

	/**
	 * 查询诗词诗单列表
	 * 
	 * @param poemSet 诗词诗单
	 * @return 诗词诗单
	 */
	@Override
	public List<PoemSet> selectPoemSetList(PoemSet poemSet) {
		return poemSetMapper.selectPoemSetList(poemSet);
	}

	/**
	 * 新增诗词诗单
	 * 
	 * @param poemSet 诗词诗单
	 * @return 结果
	 */
	@Override
	public int insertPoemSet(PoemSet poemSet) {
		poemSet.setCreateTime(DateUtils.getNowDate());
		poemSet.setCreateBy(ShiroUtils.getLoginName());
		return poemSetMapper.insertPoemSet(poemSet);
	}

	/**
	 * 修改诗词诗单
	 * 
	 * @param poemSet 诗词诗单
	 * @return 结果
	 */
	@Override
	public int updatePoemSet(PoemSet poemSet) {
		poemSet.setUpdateTime(DateUtils.getNowDate());
		poemSet.setUpdateBy(ShiroUtils.getLoginName());
		return poemSetMapper.updatePoemSet(poemSet);
	}

	/**
	 * 删除诗词诗单对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deletePoemSetByIds(String ids) {
		String[] datas = Convert.toStrArray(ids);
		for (String data : datas) {
			long setId = Long.parseLong(data);
			PoemSetDetail poemSetDetail = new PoemSetDetail();
			poemSetDetail.setSetId(setId);
			List<PoemSetDetail> list = poemSetDetailMapper.selectPoemSetDetailList(poemSetDetail);
			if (!list.isEmpty()) {
				throw new BusinessException("该诗单下有诗词列表，请先移除");
			}
		}
		return poemSetMapper.deletePoemSetByIds(datas);
	}

	/**
	 * 删除诗词诗单信息
	 * 
	 * @param id 诗词诗单ID
	 * @return 结果
	 */
	@Override
	public int deletePoemSetById(Long id) {
		return poemSetMapper.deletePoemSetById(id);
	}
}
